package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysMenuService;
import com.atguigu.spzx.manager.service.SysUserService;
import com.atguigu.spzx.manager.service.ValidateCodeService;
import com.atguigu.spzx.model.dto.system.LoginDto;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.entity.system.SysUserThreadLocalAuthContextUtil;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.system.LoginVo;
import com.atguigu.spzx.model.vo.system.SysMenuVo;
import com.atguigu.spzx.model.vo.system.ValidateCodeVo;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/admin/system/index")
@RestController
@CrossOrigin(allowCredentials = "true" , originPatterns = "*" , allowedHeaders = "*")
public class IndexController {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private ValidateCodeService validateCodeService;

    @GetMapping("/getUserInfo")
    public Result<SysUser> getUserInfo(@RequestHeader String token){
        //注意哦,这里这样写是因为api文档上写的是Header请求参数. 在之前还没遇到过的.
        //String token = request.getHeader("token");
        SysUser userInfo = sysUserService.getUserInfo(token);
        return Result.ok(userInfo);
    }

    @PostMapping("/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginInfo){
        LoginVo loginvo = sysUserService.login(loginInfo);
         //判断传过来的数据是否为空
        Assert.notNull(loginvo,"用户名或密码错误");
         return Result.ok(loginvo);
    }

    @GetMapping("/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode(){
        ValidateCodeVo validateCodeVo=validateCodeService.generateValidateCode();
        return Result.ok(validateCodeVo);
    }

    /**
     * 根据SysMenuVo的属性来看,需要多表联查了
     * 这个api是动态显示菜单,动态是根据当前用户的菜单项来动态显示的
     * 当前用户-用户角色-角色菜单 -菜单表   多表联查.
     * @return
     */
    @GetMapping("/menus")
    public Result<List<SysMenuVo>> menus(){
        List<SysMenuVo> sysMenuVoList= sysMenuService.menus();
        return Result.ok(sysMenuVoList);
    }

}
